package com.boc.address.activity;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.OptionsPickerView;
import com.boc.address.R;
import com.boc.address.R2;
import com.boc.address.bean.AddressListBean;
import com.boc.address.contract.AddAddressContract;
import com.boc.address.presenter.AddAddressPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import demo.bocweb.com.sdk.base.BasePresenter;
import demo.bocweb.com.sdk.base.activity.BaseMVPActivity;
import demo.bocweb.com.sdk.baseApi.DistrictBean;
import demo.bocweb.com.sdk.baseApi.RequestHelper;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.manager.MessageEvent;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.StringUtils;
import demo.bocweb.com.sdk.util.ToastyUtil;
import demo.bocweb.com.sdk.util.ToolbarHelper;

/**
 * 添加地址
 * Created by zhelun on 2018/2/1.
 */
@Route(path = ARoutConfig.addAddressPath)
public class AddAddressActivity extends BaseMVPActivity<AddAddressContract.IAddAddressPresenter, AddAddressContract.IAddAddressModel>
        implements AddAddressContract.IAddAddressView {
    @BindView(R2.id.et_name)
    EditText etName;
    @BindView(R2.id.et_phone)
    EditText etPhone;
    @BindView(R2.id.et_code)
    EditText etCode;
    @BindView(R2.id.tv_city)
    TextView tvCity;
    @BindView(R2.id.et_address)
    EditText etAddress;
    @BindView(R2.id.btn_commit)
    Button btnCommit;
    private List<Integer> chooseIds = new ArrayList<>();
    private List<String> chooseNames = new ArrayList<>();
    private String AddressId = "";
    private Boolean isDefault = false;
    AddressListBean bean;

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return new AddAddressPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initData() {
        super.initData();
        bean = (AddressListBean) getIntent().getSerializableExtra("addressBean");
        if (bean == null) {
            ToolbarHelper.init(this, "添加收货地址", R.mipmap.ic_back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            ToolbarHelper.init(this, "编辑收货地址", R.mipmap.ic_back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            AddressId = bean.getAddressId() + "";
            chooseIds.add(bean.getProvince());
            chooseIds.add(bean.getCity());
            chooseIds.add(bean.getDistrict());
            chooseNames.add(bean.getProvinceName());
            chooseNames.add(bean.getCityName());
            chooseNames.add(bean.getDistrictName());
            etName.setText(bean.getContact());
            etCode.setText(bean.getCode());
            etAddress.setText(bean.getAddress());
            etPhone.setText(bean.getPhone());
            isDefault = bean.isIsDefault();
            tvCity.setText(bean.getProvinceName() + bean.getCityName() + bean.getDistrictName());
            etName.setSelection(etName.getText().length());
        }

        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkNull()) {
                    commitData();
                }
            }
        });
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDistrictBean();
            }
        });
    }

    private void commitData() {
        RequestHelper request = RequestHelper.build();
        if (!AddressId.equals("")) {
            request.add("AddressId", AddressId);
        }
        request.add("Province", chooseIds.get(0) + "");
        request.add("City", chooseIds.get(1) + "");
        request.add("District", chooseIds.get(2) + "");
        request.add("ProvinceName", chooseNames.get(0));
        request.add("CityName", chooseNames.get(1));
        request.add("DistrictName", chooseNames.get(2));
//        request.add("RegionName", chooseNames.get(0) + "," + chooseNames.get(1) + "," + chooseNames.get(2));
        request.add("Address", etAddress.getText().toString().trim());
        request.add("Contact", etName.getText().toString().trim());
        request.add("Phone", etPhone.getText().toString().trim());
        request.add("Code", etCode.getText().toString().trim());
        request.add("IsDefault", isDefault);
        Loading.show(this, "上传中");
        mPresenter.addAddress(request.create());
    }

    private boolean checkNull() {
        if (StringUtils.isEmpty(etName.getText().toString())) {
            ToastyUtil.showWarning("请填写收货人姓名");
            return false;
        }
        if (StringUtils.isEmpty(etPhone.getText().toString())) {
            ToastyUtil.showWarning("请填写手机号码");
            return false;
        }

        if (!StringUtils.isPhoneNumberValid(etPhone.getText().toString())) {
            ToastyUtil.showWarning("手机号不合法");
            return false;
        }

        if (StringUtils.isEmpty(etCode.getText().toString())) {
            ToastyUtil.showWarning("请填写邮政编码");
            return false;
        }
        if (StringUtils.isEmpty(tvCity.getText().toString())) {
            ToastyUtil.showWarning("请选择地区");
            return false;
        }
        if (StringUtils.isEmpty(etAddress.getText().toString())) {
            ToastyUtil.showWarning("请填写详细地址");
            return false;
        }
        return true;
    }

    @Override
    public void addSuccess() {
        ToastyUtil.showSuccess("添加完成");
        EventBus.getDefault().post(new MessageEvent("addAddress"));
        finish();


    }

    @Override
    public void updateDistrict(final DistrictBean bean) {
        hiddenKeyboard();
        showCityChoosePop(bean);
    }

    private void showCityChoosePop(DistrictBean bean) {
        final List<String> provices = new ArrayList<>();
        final List<Integer> proviceIds = new ArrayList<>();
        final List<List<String>> citys = new ArrayList<>();
        final List<List<Integer>> cityIds = new ArrayList<>();
        final List<List<List<String>>> areaes = new ArrayList<>();
        final List<List<List<Integer>>> areaIdes = new ArrayList<>();
        for (int i = 0; i < bean.getDistrictViewList().size(); i++) {
            provices.add(bean.getDistrictViewList().get(i).getName());
            proviceIds.add(bean.getDistrictViewList().get(i).getId());
            List<String> city = new ArrayList<>();
            List<Integer> cityId = new ArrayList<>();
            List<List<String>> areas = new ArrayList<>();
            List<List<Integer>> areaIds = new ArrayList<>();
            for (int j = 0; j < bean.getDistrictViewList().get(i).getSubList().size(); j++) {
                List<String> area = new ArrayList<>();
                List<Integer> areaId = new ArrayList<>();
                city.add(bean.getDistrictViewList().get(i).getSubList().get(j).getName());
                cityId.add(bean.getDistrictViewList().get(i).getSubList().get(j).getId());
                for (int k = 0; k < bean.getDistrictViewList().get(i).getSubList().get(j).getSubList().size(); k++) {
                    area.add(bean.getDistrictViewList().get(i).getSubList().get(j).getSubList().get(k).getName());
                    areaId.add(bean.getDistrictViewList().get(i).getSubList().get(j).getSubList().get(k).getId());
                }
                areas.add(area);
                areaIds.add(areaId);

            }
            citys.add(city);
            cityIds.add(cityId);
            areaes.add(areas);
            areaIdes.add(areaIds);
        }

        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = provices.get(options1) + citys.get(options1).get(option2) + areaes.get(options1).get(option2).get(options3);
                chooseIds.add(proviceIds.get(options1));
                chooseIds.add(cityIds.get(options1).get(option2));
                chooseIds.add(areaIdes.get(options1).get(option2).get(options3));
                chooseNames.add(provices.get(options1));
                chooseNames.add(citys.get(options1).get(option2));
                chooseNames.add(areaes.get(options1).get(option2).get(options3));

                tvCity.setText(tx);
            }
        })      .setLineSpacingMultiplier(2)
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText("城市选择")//标题
                .setSubCalSize(14)//确定和取消文字大小
                .setTitleSize(16)//标题文字大小
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFFFFFFFF)//标题背景颜色 Night mode
                .setBgColor(0xFFFFFFFF)//滚轮背景颜色 Night mode
                .setContentTextSize(14)//滚轮文字大小
                .setLinkage(true)//设置是否联动，默认true
                .setLabels("", "", "")//设置选择的三级单位
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(0, 0, 0)  //设置默认选中项
                .setOutSideCancelable(true)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .build();

        pvOptions.setPicker(provices, citys, areaes);//添加数据源
        pvOptions.show();
    }

    @Override
    public void getDistrictBean() {
        mPresenter.getDistrictBean();
    }

}
